﻿using Acme.BookStore.CustomerTable;
using Acme.BookStore.CustomerTables;
using Acme.BookStore.Goodss;
using Acme.BookStore.GoodTable;
using HIS;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.Customers
{
    
    [ApiExplorerSettings(GroupName = "v1")]
    public class CustomerServices : ApplicationService, ICustomerServices
    {
        public IRepository<Customer> RoomCodeInfoRepository { get; }
        public CustomerServices(IRepository<Customer> RoomCodeInfoRepository)
        {
            this.RoomCodeInfoRepository = RoomCodeInfoRepository;
        }

        /// <summary>
        /// 客户信息添加
        /// </summary>
        /// <param name="roomCodeInfoeDTO"></param>
        /// <returns></returns>
        [HttpPost("api/CustomerAdd")]
        public async Task<APIResult> CustomerAdd(CustomersDTO roomCodeInfoeDTO)
        {
            try
            {
                Customer roomCodeInfo = ObjectMapper.Map<CustomersDTO, Customer>(roomCodeInfoeDTO);
                await RoomCodeInfoRepository.InsertAsync(roomCodeInfo);
                if (roomCodeInfo == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "添加失败"
                    };
                }
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "客户信息添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 客户信息修改
        /// </summary>
        /// <param name="roomCodeInfoeDTO"></param>
        /// <returns></returns>
        [HttpPost("api/CustomerUpdate")]
        public async Task<APIResult> CustomerUpdate(CustomersUpdateDTO roomCodeInfoUpdateDTO)
        {
            try
            {
                var roomType = await RoomCodeInfoRepository.FirstOrDefaultAsync(x => x.Id == roomCodeInfoUpdateDTO.Id);
                if (roomType == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "客户未找到"
                    };
                }

                ObjectMapper.Map(roomCodeInfoUpdateDTO, roomType);
                await RoomCodeInfoRepository.UpdateAsync(roomType);

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "客户信息修改成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"修改失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 客户信息列表
        /// </summary>
        /// <param name="roomCodeInfoeDTO"></param>
        /// <returns></returns>
        [HttpGet("api/CustomerInfoShow")]
        public async Task<APIResult<List<CustomersDTO>>> CustomerInfoShow(string CustomerName)
        {
            try
            {
                var list = await RoomCodeInfoRepository.GetListAsync(x => x.IsDeleted == false);
                if (CustomerName != null)
                {
                    list = list.Where(x => x.CustomerName == CustomerName).ToList();
                }
                return new APIResult<List<CustomersDTO>>()
                {
                    Code = CodeEnum.success,
                    Message = "查询成功",
                    Data = ObjectMapper.Map<List<Customer>, List<CustomersDTO>>(list.ToList())
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpDelete("api/CustomerDelete")]
        public async Task<APIResult> CustomerDelete(string Ids)
        {
            try
            {
                var idList = Ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                var roomTypes = await RoomCodeInfoRepository.GetListAsync(x => idList.Contains(x.Id));

                if (roomTypes == null || !roomTypes.Any())
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "删除失败，未找到指定的客户"
                    };
                }

                foreach (var roomType in roomTypes)
                {
                    await RoomCodeInfoRepository.DeleteAsync(roomType);
                }

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "删除成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"删除失败: {ex.Message}"
                };
            }
        }
    }
}
